<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/10/9
 * Time: 11:48
 */

namespace App\Models\goods\observe;


use App\Jobs\goods\Reward;
use App\Jobs\goods\SeckillReward;
use App\Models\fund\Rebate;
use App\Models\goods\Order;
use App\Models\goods\OrderDetail;
use App\Models\User;
use App\Models\user\UserSuperior;

class OrderObserve
{
    public function creating(Order $order)
    {

    }


    public function created(Order $order)
    {


        $this->handler($order);

    }


    public function updated(Order $order)
    {
        $this->handler($order);


    }

    public function deleted(Order $order)
    {



    }

    public function updating(Order $order)
    {


    }

    public function handler(Order $order)
    {
        if ($order->getOriginal('status') != $order->status){//订单状态发生改变
            OrderDetail::whereOrderId($order->id)->update(['status'=>$order->status]);
        }

        if ($order->status == 1 && $order->getOriginal('status') != $order->status){ //付完款

            User::whereId($order->user_id)->increment('sum',$order->total_price);

            foreach ($order->details as $detail){
                $detail->goods->stock -= $detail->num;

//                if ($detail->goods->type == 3){
//                    $detail->goods->seckill_stock += $detail->num;
//                }

                $detail->goods->save();
//                if ($order->user->superior_id){ //拥有上级
//
//                    switch ($detail->goods->type){
//                        case 1://主产品
////                    case 2://门店产品
//                            Reward::dispatch($order->user,$detail);
//                            break;
//                        default://秒杀产品 其它 耗材
//                            SeckillReward::dispatch($order->user,$detail);
//                            break;
//                    }
//
//                }
            }


        } elseif ($order->status == 3 && $order->getOriginal('status') != $order->status){//订单完成
            if ($order->user->superior_id && ($order->user->superior->cate >= 2
//                    || $order->user->superior->cate == 3
                )){
                //购买商品用户存在上级且上级cate为导购或业务员  进行一级返佣

                foreach ($order->details as $detail){
                    if ($detail instanceof OrderDetail);

                    if ($detail->goods->detail->is_zj == 1){//正价返佣
                        $ratio = $detail->goods->detail->zj_rebate_ratio1;
                        $ratio2 = $detail->goods->detail->zj_rebate_ratio2;
                    }else{
                        $ratio = $detail->goods->detail->hd_rebate_ratio1;
                        $ratio2 = $detail->goods->detail->hd_rebate_ratio2;
                    }

                    $map = [];
                    if ($ratio != 0){
                        //导购返佣
                        $map['user_id'] = $order->user->superior_id;
                        $map['relation_user_id'] = $order->user->id;
                        $map['order_id'] = $order->id;
                        $map['order_detail_id'] = $detail->id;
                        $map['type'] = 1;
                        $map['lv'] = 1;
                        $map['amount'] =  bcmul($detail->total_price,$ratio,2);
                        $map['status'] = 1;
                        $map['title']  = '购买商品';

                        Rebate::create($map);
                        User::whereId($order->user->superior_id)->increment('money',$map['amount']);
                        UserSuperior::where([
                            'superior_id' => $order->user->superior_id,
                            'user_id' => $order->user_id
                        ])->increment('rebate',$map['amount']);
                    }
                    $map = [];
                    if ($ratio2 != 0){
                        if ($order->user->superior->superior_id && $order->user->superior->superior->cate == 3){
                            //上级的上级为业务员  进行  2级返佣
                            $map['user_id'] = $order->user->superior->superior_id;
                            $map['relation_user_id'] = $order->user->id;
                            $map['order_id'] = $order->id;
                            $map['order_detail_id'] = $detail->id;
                            $map['type'] = 1;
                            $map['lv'] = 2;
                            $map['amount'] =  bcmul($detail->total_price,$ratio2,2);
                            $map['status'] = 1;
                            $map['title']  = '购买商品';

                            Rebate::create($map);
                            User::whereId($order->user->superior->superior_id)->increment('money',$map['amount']);
                            UserSuperior::where([
                                'superior_id' => $order->user->superior->superior_id,
                                'user_id' => $order->user->superior_id
                            ])->increment('rebate',$map['amount']);
                        }

                    }

                }

            }

        }
    }

}